﻿using Blog190311.Models.DBModel;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Blog190311.Models.DAL
{
    public class PhotoService
    {
        public List<UIData> GetPhotoMain(int page, int limit, string title)//查询所有相册
        {

            List<UIData> list = new List<UIData>();
            string sqlWhere = "";
            if (!string.IsNullOrWhiteSpace(title))
            {
                sqlWhere += " and title like '%" + title + "%'";
            }
            string sql = string.Format("select top({1})ph.Id,ph.Cont,ph.PicUrl,ar.Title from Articles ar inner join Photos ph on ph.BelongToArticles=ar.Id where ar.ShowType= '相册' and ar.IsPub = '是' and ph.BelongToArticles not in( select top({0}*{1}) BelongToArticles from Photos where BelongToArticles in (select id from Articles where ShowType = '相册' and IsPub = '是'))", page - 1, limit);
            sql = sql + sqlWhere;
            SqlDataReader dr = DBHelper.ExecuteReader(sql);
            while (dr.Read())
            {
                list.Add(new UIData()
                {
                    Id = Convert.ToInt32(dr["Id"]),
                    Title = dr["Title"].ToString(),
                    PicUrl = dr["PicUrl"].ToString(),
                    Cont = dr["Cont"].ToString()
                });
            }
            return list;
        }

        public List<UIData> GetPhotoById(int id)//相册详情
        {
            List<UIData> list = new List<UIData>();
            string sql = string.Format("select * from Photos where BelongToArticles = (select BelongToArticles from Photos where id = {0})", id);
            SqlDataReader dr = DBHelper.ExecuteReader(sql);
            while (dr.Read())
            {
                list.Add(new UIData()
                {
                    PicUrl = dr["PicUrl"].ToString(),
                    Title = dr["Cont"].ToString()
                });
            }
            return list;
        }

        public Article GetArticleDetail(int id)//轮播详情
        {
            Article article = new Article();
            string sql = string.Format("select Title,Cont from Articles where id=(select BelongToArticles from  Photos where id={0})", id);
            SqlDataReader dr = DBHelper.ExecuteReader(sql);
            while (dr.Read())
            {
                article = new Article
                {
                    Title = dr["Title"].ToString(),
                    Cont = dr["Cont"].ToString()
                };
            }
            return article;
        }

        public static List<Article> GetArticleGallery()//我的相册
        {
            List<Article> list = new List<Article>();
            string sql = "select art .Id ,art .Title ,art .SimpleInfo ,art .MainPicUrl from Articles art inner join RecArticles rec on art.Id = rec.ArticleId where art .ShowType = '相册' and art .IsPub = '是' and rec .PosId = 1 order by rec.Weight desc";
            SqlDataReader dr = DBHelper.ExecuteReader(sql);
            while (dr.Read())
            {
                list.Add(new Article
                {
                    Id = Convert.ToInt32(dr["Id"]),
                    MainPicUrl = dr["MainPicUrl"].ToString(),
                    Title = dr["Title"].ToString(),
                    SimpleInfo=dr["SimpleInfo"].ToString ()
                });
            }
            return list;
        }
    }
}